package cn.life.business.tool.controller;

import cn.life.common.entity.model.CMessage;
import cn.life.business.tool.entity.po.Tools;
import cn.life.business.tool.entity.vo.ToolsVo;
import cn.life.business.tool.service.ToolService;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description:    工具借还控制层
 * @author wangzhenyu
 * @Date 2018/7/6
 */
@RestController
@RequestMapping("tool")
public class ToolController {

    @Autowired
    private ToolService toolService;

    /**
     * @Description:  工具借还记录添加
     * @param   tools
     * @return
     */
    @PostMapping(value = "/addtool")
    public CMessage addTool(@RequestBody Tools tools, @MyUser MyUserInfo myUserInfo){
        getOprDate(tools,myUserInfo);
        return toolService.addTool(tools);
    }

    /**
     * @Description:  工具借还记录删除
     * @param   tools
     * @return
     */
    @PostMapping(value = "/deletetool")
    public CMessage deleteTool(@RequestBody Tools tools, @MyUser MyUserInfo myUserInfo){
        //getOprDate(tools,myUserInfo);
        return toolService.deleteTool(tools.getToolId());
    }

    /**
     * @Description:  工具借还记录修改
     * @param   tools
     * @return
     */
    @PostMapping(value = "/updatetool")
    public CMessage updateTool(@RequestBody Tools tools){
        return toolService.updateTool(tools);
    }

    /**
     * @Description:  工具借还记录归还状态修改
     * @param   tools
     * @return
     */
    @PostMapping(value = "/updatestatetool")
    public CMessage updateToolState(@RequestBody Tools tools){
        return toolService.updateToolState(tools);
    }

    /**
     * @Description:  工具借还记录查询
     * @param   toolsVo
     * @return
     */
    @PostMapping(value = "/getalltoolinfo")
    public CMessage getAllToolInfo(@RequestBody ToolsVo toolsVo,@MyUser MyUserInfo myUserInfo) throws ParseException {
        if(toolsVo.getProjectId() == null){
            toolsVo.setProjectId(myUserInfo.getProjectId());
        }
        return toolService.getAllToolsInfo(toolsVo);
    }

    /**
     * @Description:  单条查询（详情）
     * @param   toolsVo
     * @return
     */
    @PostMapping(value = "/gettool")
    public  CMessage getTool(@RequestBody ToolsVo toolsVo){
        return toolService.getTool(toolsVo);
    }

    /**
     * @Description:  自动添加项
     * @param   tools,myUserInfo
     * @return
     */

    public Tools getOprDate(Tools tools,MyUserInfo myUserInfo){
        tools.setStillState(0);
        tools.setStartDate(new Date());
        tools.setProjectId(myUserInfo.getProjectId());
        tools.setOprUserId(myUserInfo.getUserId());
        tools.setOprUserName(myUserInfo.getUserName());
        tools.setOprDate(new Date());
        return tools;
    }
}

